package com.CGFrame.Axis;


import java.util.Iterator;
import java.util.Set;

import org.apache.axis.MessageContext;
import org.apache.axis.session.Session;
import org.jdom.Document;
import org.jdom.Element;




import com.CGFrame.struts.POJOs.Project;
import com.CGFrame.struts.POJOs.Reguser;
import com.CGFrame.struts.bean.ProjectOperate;
import com.CGFrame.struts.bean.ProjectOperateFactory;
import com.CGFrame.struts.bean.UserLogin.*;

public class UserLogin {
	public String LoginValidate(String RegName,String PassWord)
	{
	
	//	String XMLStr="";
		LoginValidate lv = new LoginValidate();
		String status = lv.LoginCheck(RegName,PassWord);
	//	status = "2";
		MessageContext mc = MessageContext.getCurrentContext();
		Session session = mc.getSession(); 
	//	session.set("TEST", "TEST");
		
		if(!status.equals("NoUser")&&!status.equals("False"))
		{
			ProjectOperate pop = ProjectOperateFactory.getprojectOperate();
			int UserID = Integer.parseInt(status);
			Reguser r =  (Reguser)pop.UniqueQuery("Reguser", "regUserId,"+UserID);
			LoginBean lb = new LoginBean(r);

			Element elementRoot = new Element("Result");
			elementRoot.addAttribute("Status", "SUCCESS");
		//	elementRoot.addAttribute("RegUserId",r.getRegUserId().toString());
			///set Session
		//	 mc = MessageContext.getCurrentContext();
		//	 session = mc.getSession(); 
			session.set("RegUserId", r.getRegUserId().toString());
			Document document = new Document(elementRoot);
			Iterator ite_Projects = lb.getProjects().iterator();
			Element element ;
			while (ite_Projects.hasNext())
			{
				element= new Element("Project");
				Project project = (Project)ite_Projects.next();

				element.addAttribute("ProjectId",project.getProjectId().toString());
				element.addAttribute("ProjectName",project.getProjectName());
				document.getRootElement().addContent(element);
				
			}
			
			XMLOut out= new XMLOut();
			String XMLStr =  out.outputToString(document);
			System.out.println(XMLStr);
			return XMLStr;
			
		}
		else
		{
			return null;
		}		
	}

	

}
